using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;

namespace STHPMS1.Common.C_Utils
{
	public class MyCacheTools
	{
		#region 存入Cache
		/// <summary>
		/// 存入Cache
		/// </summary>
		/// <param name="key">缓存key</param>
		/// <param name="value">缓存的值</param>
		/// <param name="time_HH">存xx小时</param>
		/// <returns>是否执行成功[bool]</returns>
		public static bool SetCache (string key, object value, int time_HH)
		{
			bool result = false;
			try {
				DateTime dt = DateTime.Now.AddHours (time_HH);
				System.Web.HttpRuntime.Cache.Insert (key, value, null, dt, System.Web.Caching.Cache.NoSlidingExpiration);
				result = true;
			}
			catch (System.Exception) {
			}
			return result;
		}

		#endregion

		#region 取得Cache
		/// <summary>
		/// 取得Cache
		/// </summary>
		/// <param name="key">key</param>
		/// <returns>object类型</returns>
		public static object GetCache (string key)
		{
			return System.Web.HttpRuntime.Cache.Get (key);
		}

		#endregion

		public static void ClearCache (string key)
		{
			if (IsCacheExist (key)) {
				System.Web.HttpRuntime.Cache.Remove (key);
			}
		}

        public static void ClearAllCache()
        {
            List<string> cacheKeys = new List<string>();
            IDictionaryEnumerator cacheEnum = HttpContext.Current.Cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                cacheKeys.Add(cacheEnum.Key.ToString());
            }
            foreach (string cacheKey in cacheKeys)
            {
                HttpContext.Current.Cache.Remove(cacheKey);
            }
        }

		#region 查询Cache是否存在
		/// <summary>
		/// 查询Cache是否存在
		/// </summary>
		/// <param name="key">key 值</param>
		/// <returns>bool</returns>
		public static bool IsCacheExist (string key)
		{
			bool result = false;
			object temp = System.Web.HttpRuntime.Cache.Get (key);
			if (null != temp) {
				result = true;
			}
			return result;
		}
	#endregion
	}
}
